package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Business;
import com.aaa.tmr.entity.PageBean;
import com.aaa.tmr.entity.ReturnBean;
import  com.aaa.tmr.entity.Dictionary;
import com.aaa.tmr.service.BusinessService;
import com.aaa.tmr.service.DictionaryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.github.pagehelper.PageInfo;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Business)表控制层
 *
 * @author makejava
 * @since 2021-03-24 16:07:52
 */
@Controller
@RequestMapping("business")
public class BusinessController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessService businessService;
    @Resource
    private DictionaryService dictionaryService;

    @Resource
    private HttpSession session;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Business selectOne(Integer id) {
        return this.businessService.queryById(id);
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toBusiness")
    public String toPage() {
        return "back/business-page";
    }
    /**
     * 跳转到回访图表分析页面
     * * @return
     */
    @RequestMapping("toBusinessEchart")
    public String toPageEchart() {
        return "back/businessEchart";
    }
    /**
     * 跳转到回访图表分析页面
     * * @return
     */
    @RequestMapping("toBusinessEchartpage")
    public String toPageEchartpage() {
        return "attendanceEchart";
    }
    /**
     * @return
     * @create by: ydq
     * @description: 方法作用：业务分析按时间
     * @create time: 2020/12/30 20:33
     * @param:
     */
    @GetMapping("showBusinessChartByAgent")
    @ResponseBody
    public ReturnBean showBusinessChartByAgent(String loginName) {
        Map<String, Object> map = businessService.showBusinessChartByAgent(loginName);
        return returnSuccess(map);
    }
    /**
     * @return
     * @create by: ydq
     * @description: 方法作用：业务分析按代理人
     * @create time: 2020/12/30 20:03
     * @param:
     */
    @RequestMapping("showAgentChart")
    @ResponseBody
    public ReturnBean showAgentChart(String startTime, String endTime) {
        Map<String, Object> map = businessService.showAgentChart(startTime, endTime);
        return returnSuccess(map);
    }

    /**
     * 分页查询方法
     *
     * @param page
     * @param limit
     * @param business
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Business business) {
        PageInfo pageInfo = businessService.queryAll(page, limit, business);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * 部门添加
     *
     * @param business
     * @return
     */
    @ResponseBody
    @PostMapping("insert")
    public Result insert(Business business) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, businessService.insert(business));
    }

    /**
     * 部门更新
     *
     * @param business
     * @return
     */
    @ResponseBody
    @PutMapping("update")
    public Result update(Business business) {
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0, businessService.update(business));
    }

    /**
     * 通过主键删除数据
     *
     * @param businessId 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteById")
    public Result deleteById(Integer businessId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.businessService.deleteById(businessId));
    }


    /**
     * 通过主键删除数据
     *
     * @param businessIds 主键
     * @return 是否成功
     */
    @ResponseBody
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(String businessIds) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.businessService.batchDeleteById(businessIds));
    }
//    /**
//     * 按年月查询图标显示数据
//     * @param year
//     * @param month
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("queryShowChartData")
//    public Result queryShowChartData(Integer year, Integer month) {
//        return new Result(ReturnStatus.SUCCESS.getCode(),
//                ReturnStatus.SUCCESS.getMessage(), 0,
//                this.businessService.queryShowChartData(year,month));
//    }
    /**
     * @param :
     * @return com.cloudwise.trademark.entity.ReturnBean
     * @create by: ydq
     * @description : 业务分析
     * @create time: 2020/12/29 11:57
     */
    @ResponseBody
    @GetMapping("businessEchart")
    public ReturnBean businessEchart() {
        List<Long> longs = this.businessService.businessEchart();
        return returnSuccess(longs);
    }

    /**
     * @return
     * @create by: Back
     * @description: 查询所有进度类型
     * @create time: 2020/12/30 14:10
     */
    @ResponseBody
    @GetMapping("selectProcessType")
    public ReturnBean selectProcessType(PageBean pageBean, Dictionary dictionary) {
        int offset = getOffset(pageBean);
        List<Dictionary> dictionaries = dictionaryService.queryAllByConditionAndLimit(dictionary, offset, pageBean.getLimit());
        long rowCount = dictionaryService.getRowCount(dictionary);
        return returnSuccess(dictionaries, rowCount);
    }
    /**
     * @param :
     * @return com.cloudwise.trademark.entity.ReturnBean
     * @create by: IvanZ
     * @description : 进度分析，以代理人为横轴
     * @create time: 2020/12/29 11:57
     */
    @ResponseBody
    @GetMapping("progressEchartName")
    public ReturnBean progressEchartName(String progressId, String startTime, String endTime) {

        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        if (progressId != null && !"".equals(progressId)) {
            int i = Integer.parseInt(progressId);
            Dictionary dictionary = dictionaryService.queryById(i);

            Map<String, Object> map = businessService.progressEchartName(startTime, endTime, dictionary.getDictionaryName());
            return returnSuccess(map);
        } else {
            Map<String, Object> map = businessService.progressEchartName(startTime, endTime, null);
            return returnSuccess(map);
        }
    }


    /**
     * @return
     * @create by: Back
     * @description: 进度分析，以时间为横轴
     * @create time: 2020/12/30 10:12
     */
    @ResponseBody
    @GetMapping("progressEchartTime")
    public ReturnBean progressEchartTime(String loginName, String processId) {

        if (processId != null && !"".equals(processId)) {
            int i = Integer.parseInt(processId);
            Dictionary dictionary = dictionaryService.queryById(i);
            Map<String, Object> map = businessService.progressEchartTime(loginName, dictionary.getDictionaryName());
            return returnSuccess(map);
        } else {
            Map<String, Object> map = businessService.progressEchartTime(loginName, null);
            return returnSuccess(map);
        }
    }
    /**
     * @param :
     * @return com.cloudwise.trademark.entity.ReturnBean
     * @create by: IvanZ
     * @description : 回访分析根据代理人
     * @create time: 2020/12/29 11:57
     */
    @ResponseBody
    @GetMapping("visitEchartName")
    public ReturnBean visitEchartName(String startTime, String endTime) {
        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        Map<String, Object> map = businessService.showVisitEchartName(startTime, endTime);
        return returnSuccess(map);
    }

    /**
     * @param :
     * @return com.cloudwise.trademark.entity.ReturnBean
     * @create by: IvanZ
     * @description : 回访分析根据时间
     * @create time: 2020/12/30 10:04
     */
    @ResponseBody
    @GetMapping("visitEchartTime")
    public ReturnBean visitEchartTime(String loginName) {
        if (loginName == null || "".equals(loginName)) {
            loginName = "enzo";
        }
        Map<String, Object> map = businessService.showVisitEchartTime(loginName);
        return returnSuccess(map);
    }



}
